from sqlalchemy import Column, String, create_engine, BIGINT, Integer, Text
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from domain.domain import Catalog,Plant
from dao.CatalogDAO import CatalogDAO
from dao.PlantDAO import  PlantDAO
from helper.ConnectionHelper import ConnectionHelper
import os
import re
from lxml import etree
import requests
import pymysql
import sys
from bs4 import BeautifulSoup


# 抓取对应的植物网页

class SpiderFrps:
    # 植物志按名称抓取Url
    catchNameURL = "http://frps.eflora.cn/frps?id="
    session = None

    # # 初始化
    # def __init__(self):
    #     # 创建session,一次创建,整个类都会使用它
    #     session = ConnectionHelper.GetSession()

    # 根据植物的名字取对应的数据
    def FetchByPlantName(self, plantName):
        session = ConnectionHelper.GetSession()

        # 1)取html数据
        arrayCata = []
        headers = {
            "Accept-Encoding": "gzip, deflate",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
            "Connection": "keep-alive",
            "Upgrade-Insecure-Requests": "1",
            "Host": "frps.eflora.cn",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"
        }

        url = "http://frps.eflora.cn/frps?id=" + plantName
        response = requests.get(url, headers=headers)
        bs = BeautifulSoup(response.text, 'lxml')
        # 2)取右边的目录,放入到arrayCata中,
        # 示例:[['被子植物门', 'Angiospermae'],... ['短花冠管组', 'Sect.', 'Ligustrina']]
        node = bs.select(".divrightcon")
        link = node[0].find_all("span")
        catalogDao = CatalogDAO()

        for i in range(len(link)):
            arr = link[i].text.split()
            arrayCata.append(arr)

        # 3)判断目录是否已经存在于catalog表中,如果没有,那么加入它
        parentId = 1  # 默认的parentid为1表示是植物
        fullParentName = "植物界"
        for j in range(len(arrayCata)):

            item = arrayCata[j]
            catalog = catalogDao.getByName(session, item[0])

            # 3.1) 目录中是否存在此名称
            if (len(catalog) == 0):
                # 3.1.1)不存在,则按级别加入它,
                newCatalog = Catalog(name=item[0],
                                     parent_id=parentId,
                                     full_name=fullParentName + ">" + item[0]
                                     , level=j + 1)
                if (len(item) > 1):
                    # 有英文
                    newCatalog.eng_name = item[1]

                catalogDao.add(session, newCatalog)
                parentId = newCatalog.id
                fullParentName += ">" + newCatalog.name


            else:
                # 指向本节点,为添加下级节点作准备
                parentId = catalog[0].id
                fullParentName += ">" + item[0]

        # 4)将描述写入到plant表中

        node_2 = bs.select("#spinfoDiv")
        children = node_2[0].children
        sum = 0
        contArr = []
        contArr1 = []
        for child in children:

            if (sum == 3):
                # 真正的内容区
                trueContent = child.find_all("p")
                contArr1.append(trueContent[0].text)
                if (len(trueContent) > 2):

                    for m in range(len(trueContent)):
                        if (m >= 2):
                            contArr.append(trueContent[m].text)

            sum += 1
        # 取别名，存在contArr2里面，实例['19. 北京丁香（河北习见树木图说）', '臭多罗（河北）']，从第二个开始是别名
        length = len(contArr1[0].split('\u3000'))
        contArr2 = contArr1[0].split('\u3000')[1:length - 1]

        # 数组转字符串

        str1 = ','.join(contArr2)
        str2 = ""
        for item2 in contArr:
            str2 += "<p>" + item2 + "</p>"

        # print(contArr)

        # 4.1）判断plant是否在数据库中
        plantDao = PlantDAO()
        plant = plantDao.getByName(session, plantName)
        if (len(plant) == 0):
            # 没有这个植物，则添加
            newPlant = Plant(name=plantName,
                             alias_name=str1,
                             catalog_id=parentId,
                             catalog_name=fullParentName,
                             content=str2
                             )
            plantDao.add(session, newPlant)
        else:
            plant.alias_name = str1
            plant.catalog_id = parentId
            plant.catalog_name = fullParentName
            plant.content = str2
            plantDao.update(session, plant)

        # 5)取对应的图片,并写入到图片表中

        return 0
